package com.pig4cloud.pigx.wms.service.impl;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.wms.dto.WCSInvInDTO;
import com.pig4cloud.pigx.wms.dto.WCSInvOutDTO;
import com.pig4cloud.pigx.wms.dto.WCSResponseDTO;
import com.pig4cloud.pigx.wms.dto.WcsWmsCallBackDTO;
import com.pig4cloud.pigx.wms.dto.WmsDpInvDTO;
import com.pig4cloud.pigx.wms.dto.WmsKpInvDTO;
import com.pig4cloud.pigx.wms.entity.ERPInStorageDTO;
import com.pig4cloud.pigx.wms.entity.ERPInStorageItemDTO;
import com.pig4cloud.pigx.wms.entity.ERPInStorageTpDTO;
import com.pig4cloud.pigx.wms.entity.InStorage;
import com.pig4cloud.pigx.wms.entity.InStorageItem;
import com.pig4cloud.pigx.wms.entity.InStorageTp;
import com.pig4cloud.pigx.wms.entity.SLocation;
import com.pig4cloud.pigx.wms.entity.StockArea;
import com.pig4cloud.pigx.wms.entity.TraySku;
import com.pig4cloud.pigx.wms.entity.WcsWmsTrx;
import com.pig4cloud.pigx.wms.mapper.WcsWmsTrxMapper;
import com.pig4cloud.pigx.wms.service.ERPCallBackService;
import com.pig4cloud.pigx.wms.service.InStorageItemService;
import com.pig4cloud.pigx.wms.service.InStorageService;
import com.pig4cloud.pigx.wms.service.InStorageTpService;
import com.pig4cloud.pigx.wms.service.SLocationService;
import com.pig4cloud.pigx.wms.service.StockAreaService;
import com.pig4cloud.pigx.wms.service.TraySkuService;
import com.pig4cloud.pigx.wms.service.WcsWmsTrxService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import cn.hutool.core.date.DatePattern;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * wcs和wms交互记录
 *
 * @author gaoxin
 * @date 2021-04-15 09:50:40
 */
@Slf4j
@Service
@RefreshScope
@RequiredArgsConstructor
public class WcsWmsTrxServiceImpl extends ServiceImpl<WcsWmsTrxMapper, WcsWmsTrx> implements WcsWmsTrxService {

	private final StockAreaService stockAreaService;

	private final SLocationService sLocationService;

	private final InStorageService inStorageService;

	private final InStorageItemService inStorageItemService;

	private final InStorageTpService inStorageTpService;

	private final TraySkuService traySkuService;

	private final ERPCallBackService erpCallBackService;

	@Value("${wcs.invin.url}")
	private String invInUrl;

	@Value("${wcs.invout.url}")
	private String invOutUrl;

	@Override
	public boolean getTestWCS() {

		RestTemplate restTemplate = new RestTemplate();

		MultiValueMap<String, String> paramMap = new LinkedMultiValueMap(){
			{
				add("taskId", "gxtestrukutask0001");
				add("taskSource","1");
				add("locatorFrom", "CX100001");
				add("InLoc", "A100004");
				add("stockNo", "TP202104080945");
				add("memoInfo1", "入库备注说明1");
				add("memoInfo2","入库备注说明2");
			}
		};

		//封装请求头
		try {
			RequestEntity requestEntity = RequestEntity
					// 可以不填
					.post(new URI(""))
					// 参数传输类型 url编码格式 application/x-www-form-urlencoded
					.contentType(MediaType.APPLICATION_FORM_URLENCODED)
					.accept(MediaType.ALL).acceptCharset(StandardCharsets.UTF_8)
					.body(paramMap);
			//基于注解映射对应实体
			ResponseEntity<WCSResponseDTO> result = restTemplate.postForEntity("http://112.4.208.194:8801/api/WmsInvIn",
					requestEntity, WCSResponseDTO.class);
			WCSResponseDTO wcsResponseDTO = result.getBody();
			log.info(wcsResponseDTO.getErrorCode());
			log.info(wcsResponseDTO.getErrorMsg());
			log.info(result.toString());
		} catch (Exception e) {
			log.error("调用wcs接口出现异常:"+e.getMessage());
			e.printStackTrace();
			return false;
		}

		return false;
	}

	@Override
	public boolean handleWCSInvIn(WCSInvInDTO wcsInvInDTO) {

		RestTemplate restTemplate = new RestTemplate();

		MultiValueMap<String, String> paramMap = new LinkedMultiValueMap(){
			{
				add("taskId", wcsInvInDTO.getTaskId());
				add("taskSource",wcsInvInDTO.getTaskSource());
				add("locatorFrom", wcsInvInDTO.getLocatorFrom());
				add("InLoc", wcsInvInDTO.getInloc());
				add("stockNo", wcsInvInDTO.getStockNo());
				add("memoInfo1", wcsInvInDTO.getMemoInfo1());
				add("memoInfo2",wcsInvInDTO.getMemoInfo2());
			}
		};

		log.info("发送wcs入库请求的参数是:"+wcsInvInDTO);

		//封装请求头
		try {
			RequestEntity requestEntity = RequestEntity
					// 可以不填
					.post(new URI(""))
					// 参数传输类型 url编码格式 application/x-www-form-urlencoded
					.contentType(MediaType.APPLICATION_FORM_URLENCODED)
					.accept(MediaType.ALL).acceptCharset(StandardCharsets.UTF_8)
					.body(paramMap);
			//基于注解映射对应实体
			ResponseEntity<WCSResponseDTO> result = restTemplate.postForEntity(invInUrl,
					requestEntity, WCSResponseDTO.class);
			WCSResponseDTO wcsResponseDTO = result.getBody();

			log.info("WCS入库请求响应errorCode:"+wcsResponseDTO.getErrorCode());
			log.info("WCS入库请求响应errorMsg:"+wcsResponseDTO.getErrorMsg());

			if("0".equals(wcsResponseDTO.getErrorCode())){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			log.error("调用wcs入库接口出现异常:"+e.getMessage());
			e.printStackTrace();
			return false;
		}

	}

	@Override
	public boolean handleWCSInvOut(WCSInvOutDTO wcsInvOutDTO) {
		RestTemplate restTemplate = new RestTemplate();

		MultiValueMap<String, String> paramMap = new LinkedMultiValueMap(){
			{
				add("taskId", wcsInvOutDTO.getTaskId());
				add("invOutType",wcsInvOutDTO.getInvOutType());
				add("OutLoc", wcsInvOutDTO.getOutLoc());
				add("Locdesti", wcsInvOutDTO.getLocDesti());
				add("stockNo", wcsInvOutDTO.getStockNo());
				add("memoInfo1", wcsInvOutDTO.getMemoInfo1());
				add("memoInfo2",wcsInvOutDTO.getMemoInfo2());
			}
		};

		log.info("发送wcs出库请求的参数是:"+wcsInvOutDTO);

		//封装请求头
		try {
			RequestEntity requestEntity = RequestEntity
					// 可以不填
					.post(new URI(""))
					// 参数传输类型 url编码格式 application/x-www-form-urlencoded
					.contentType(MediaType.APPLICATION_FORM_URLENCODED)
					.accept(MediaType.ALL).acceptCharset(StandardCharsets.UTF_8)
					.body(paramMap);
			//基于注解映射对应实体
			ResponseEntity<WCSResponseDTO> result = restTemplate.postForEntity(invOutUrl,
					requestEntity, WCSResponseDTO.class);
			WCSResponseDTO wcsResponseDTO = result.getBody();

			log.info("WCS出库请求响应errorCode:"+wcsResponseDTO.getErrorCode());
			log.info("WCS出库请求响应errorMsg:"+wcsResponseDTO.getErrorMsg());

			if("0".equals(wcsResponseDTO.getErrorCode())){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			log.error("调用wcs出库接口出现异常:"+e.getMessage());
			e.printStackTrace();
			return false;
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String putEmptyTrayLocation(WmsDpInvDTO wmsDpInvDTO) {

		//获取启用的托盘区未使用的排序级别最高的库位编号
		SLocation sLocation =
				sLocationService.getOne(Wrappers.<SLocation>query().lambda().eq(SLocation::getStatus, 1)
						.eq(SLocation::getAreaId, 3).eq(SLocation::getIsUsed, 0)
						.eq(SLocation::getLocationStatus,"1")
						.orderByAsc(SLocation::getSOrder).last("limit 1"));

		if (sLocation == null) {
			return null;
		} else {
			String sNo = sLocation.getSLocationNo();
			sLocation.setIsUsed("2");
			sLocationService.updateById(sLocation);

			WcsWmsTrx wcsWmsTrx = new WcsWmsTrx();
			wcsWmsTrx.setWmsTrxId(wmsDpInvDTO.getTaskId());
			wcsWmsTrx.setTrxType("1");
			wcsWmsTrx.setFromLocation(wmsDpInvDTO.getDpDevId());
			wcsWmsTrx.setToLocation(sNo);

			this.save(wcsWmsTrx);
			return sNo;
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String getEmptyTrayLocation(WmsKpInvDTO wmsKpInvDTO) {

		//获取启用的托盘区已使用的已放置托盘的排序级别最高的库位编号
		SLocation sLocation =
				sLocationService.getOne(Wrappers.<SLocation>query().lambda().eq(SLocation::getStatus, 1)
						.eq(SLocation::getAreaId, 3)
						.eq(SLocation::getIsUsed, 1)
						.eq(SLocation::getLocationStatus,"2")
						.orderByAsc(SLocation::getSOrder).last("limit 1"));
		if (sLocation == null) {

			return null;

		} else {
			//获取1个产线入库工作区
//			SLocation sLocationWork =
//					sLocationService.getOne(Wrappers.<SLocation>query().lambda().eq(SLocation::getStatus, 1)
//							.eq(SLocation::getAreaId, 4)
//							.eq(SLocation::getIsUsed, 0)
//							.eq(SLocation::getLocationStatus,"1")
//							.orderByAsc(SLocation::getSOrder).last("limit 1"));

//
//			String to = sLocationWork.getSLocationNo();
			String from = sLocation.getSLocationNo();

			sLocation.setIsUsed("2");
			sLocationService.updateById(sLocation);

			WcsWmsTrx wcsWmsTrx = new WcsWmsTrx();
			wcsWmsTrx.setWmsTrxId(wmsKpInvDTO.getTaskId());
			wcsWmsTrx.setTrxType("2");
			wcsWmsTrx.setToLocation(wmsKpInvDTO.getKpDevId());
			wcsWmsTrx.setFromLocation(from);

			this.save(wcsWmsTrx);

			return from;
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean getEmptyTrayLocationForProduction(String trayNo) {

		//获取产线入库单中托盘信息
		InStorageTp inStorageTp =
				inStorageTpService.getOne(Wrappers.<InStorageTp>query().lambda().eq(InStorageTp::getUniqueCode,
						trayNo).eq(InStorageTp::getStatus,"0"));

		InStorageItem inStorageItem =
				inStorageItemService
						.getOne(Wrappers.<InStorageItem>query().lambda().eq(InStorageItem::getInStorageItemId,
								inStorageTp.getInStorageItemId()));

		InStorage instorage =
				inStorageService.getOne(Wrappers.<InStorage>query().lambda().eq(InStorage::getWmsBillNo,
						inStorageItem.getWmsBillNo()));

		StockArea stockArea;

		try{
			stockArea = inStorageService.getStockArea(inStorageItem, instorage);
		}catch (Exception e){
			log.error("获取库区出现异常。。。",e);
			return false;
		}
		if(null == stockArea){
			log.error("获取库区出现异常。。。没找到库区");
			return false;
		}
		//获取启用的立库区未使用的排序级别最高的库位编号
		SLocation sLocation =
				sLocationService.getOne(Wrappers.<SLocation>query().lambda().eq(SLocation::getStatus, 1)
						.eq(SLocation::getAreaId, 1).eq(SLocation::getIsUsed, 0)
						.eq(SLocation::getLocationStatus,"1")
						.eq(SLocation::getStockAreaNo,stockArea.getStockAreaNo())
						.orderByAsc(SLocation::getSOrder).last("limit 1"));
		//获取产线入库开始位置的库位编号
		SLocation sLocationFrom =
				sLocationService.getOne(Wrappers.<SLocation>query().lambda().eq(SLocation::getStatus, 1)
						.eq(SLocation::getAreaId, 4).eq(SLocation::getIsUsed, 0)
						.eq(SLocation::getLocationStatus,"1")
						.orderByAsc(SLocation::getSOrder).last("limit 1"));

		if (sLocation == null) {

			return false;

		} else {

			inStorageTp.setStatus("1");
			inStorageTpService.updateById(inStorageTp);

			String sNo = sLocation.getSLocationNo();
			sLocation.setIsUsed("2");
			sLocationService.updateById(sLocation);

			//更新产线入库单状态为正在入库
			//更新入库单信息 产线一次回调就更新成入库中
			instorage.setStatus("1");
			inStorageService.updateById(instorage);

            //调用wcs做产线入库
			WCSInvInDTO wcsInvInDTO = new WCSInvInDTO();
			String taskId = "wmstrx" + LocalDateTime.now()
					.format(DateTimeFormatter.ofPattern(DatePattern.PURE_DATETIME_MS_PATTERN));
			wcsInvInDTO.setTaskId(taskId);
			wcsInvInDTO.setTaskSource("3");
			wcsInvInDTO.setLocatorFrom(sLocationFrom.getSLocationNo());
			wcsInvInDTO.setInloc(sNo);
			wcsInvInDTO.setStockNo(trayNo);
			wcsInvInDTO.setMemoInfo1("wms调用wcs做产线入库");

			if(!this.handleWCSInvIn(wcsInvInDTO)){
				throw new RuntimeException("调用WCS入库接口异常!!!");
			}

			WcsWmsTrx wcsWmsTrx = new WcsWmsTrx();
			wcsWmsTrx.setWmsTrxId(taskId);
			wcsWmsTrx.setTrxType("3");
			wcsWmsTrx.setTrayNo(trayNo);
			wcsWmsTrx.setFromLocation(sLocationFrom.getSLocationNo());
			wcsWmsTrx.setToLocation(sNo);

			this.save(wcsWmsTrx);
			return true;
		}

	}

	@Override
	public void getWGInStorageTpForPDA() {
			SLocation fromLocation =
					sLocationService.getOne(Wrappers.<SLocation>query().lambda().eq(SLocation::getAreaId, "3")
							.eq(SLocation::getIsUsed,"1").eq(SLocation::getStatus,"1")
							.eq(SLocation::getLocationStatus,"2")
							.orderByAsc(SLocation::getSOrder).last("limit 1"));
		//获取1个外购入库工作区
		SLocation sLocationWork =
				sLocationService.getOne(Wrappers.<SLocation>query().lambda().eq(SLocation::getStatus, 1)
						.eq(SLocation::getAreaId, 6)
						.eq(SLocation::getIsUsed, 0)
						.eq(SLocation::getLocationStatus,"1")
						.orderByAsc(SLocation::getSOrder).last("limit 1"));

        if(fromLocation == null){

        	throw new RuntimeException("未找到空托盘库位");

		}

		//外购入库空托补给 调用WCS 出库接口
		WCSInvOutDTO wcsInvOutDTO = new WCSInvOutDTO();
		String taskId = "wmstrx" + LocalDateTime.now()
				.format(DateTimeFormatter.ofPattern(DatePattern.PURE_DATETIME_MS_PATTERN));
		wcsInvOutDTO.setTaskId(taskId);
		wcsInvOutDTO.setInvOutType("9");
		wcsInvOutDTO.setOutLoc(fromLocation.getSLocationNo());
		wcsInvOutDTO.setStockNo("多个空托");
		wcsInvOutDTO.setLocDesti(sLocationWork.getSLocationNo());
		wcsInvOutDTO.setMemoInfo1("wms调用wcs出库接口,用于空托出库forPDA");

		if(!this.handleWCSInvOut(wcsInvOutDTO)){
			throw new RuntimeException("调用WCS出库接口异常!!!");
		}

		log.info("wms调用wcs出库接口,用于空托出库forPDA");

		fromLocation.setIsUsed("2");
		sLocationService.updateById(fromLocation);

		WcsWmsTrx wcsWmsTrx = new WcsWmsTrx();
		wcsWmsTrx.setWmsTrxId(taskId);
		wcsWmsTrx.setTrxType("5");
		wcsWmsTrx.setToLocation(sLocationWork.getSLocationNo());
		wcsWmsTrx.setFromLocation(fromLocation.getSLocationNo());

		this.save(wcsWmsTrx);

	}

	/**
	 * statusCode	statusInfor
	 * 0	任务新建/等待执行
	 * 1	任务执行中
	 * 2	到达起始点
	 * 3	离开起始点
	 * 4	到达结束点
	 * 5	离开结束点
	 * 6	故障错误
	 * wms wcs交互类型：
	 * 1:托盘入库 2:托盘出库 3:产线入库 4:电商入库 5:空托补给PDA 6空框入库 7空框出库 8外购入库 9产线出库 10产线返库 11空托返叠 12补货出库
	 * @param wcsWmsCallBackDTO
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean wcsCallBack(WcsWmsCallBackDTO wcsWmsCallBackDTO) {

		//获取wmstrx的记录
		WcsWmsTrx wcsWmsTrx =
				this.getOne(Wrappers.<WcsWmsTrx>query().lambda().eq(WcsWmsTrx::getWmsTrxId,
						wcsWmsCallBackDTO.getWmsTrxId()));
		//获取wmstrx中from库位信息
		SLocation sLocationFrom =
				sLocationService.getOne(Wrappers.<SLocation>query().lambda().eq(SLocation::getSLocationNo,
						wcsWmsTrx.getFromLocation()));
		//获取wmstrx中目标库位信息
		SLocation sLocationTO =
				sLocationService.getOne(Wrappers.<SLocation>query().lambda().eq(SLocation::getSLocationNo,
						wcsWmsTrx.getToLocation()));
		//如果wcs返回操作成功
		if (wcsWmsCallBackDTO.getStatusCode() == 5) {
			//如果是空托入库 更新库位信息
			if ("1".equals(wcsWmsTrx.getTrxType())) {

				sLocationTO.setIsUsed("1");
				sLocationTO.setSku("101");
				sLocationTO.setLocationStatus("2");
				//更新库位信息
				sLocationService.updateById(sLocationTO);

				//更新wmstrx信息
				wcsWmsTrx.setTrxStatus("1");
				this.updateById(wcsWmsTrx);
				return true;

			} else if ("2".equals(wcsWmsTrx.getTrxType())) {
				//托盘出库
				sLocationFrom.setIsUsed("0");
				sLocationFrom.setLocationStatus("1");
				//更新库位信息
				sLocationService.updateById(sLocationFrom);

				//更新wmstrx信息
				wcsWmsTrx.setTrxStatus("1");
				this.updateById(wcsWmsTrx);
				return true;
			} else if ("3".equals(wcsWmsTrx.getTrxType()) || "4".equals(wcsWmsTrx.getTrxType()) || "8".equals(wcsWmsTrx.getTrxType())) {
				//产线入库、电商入库、外购入库
				//获取入库单中托盘信息 不等于<>2已完成状态的
				InStorageTp inStorageTp =
						inStorageTpService.getOne(Wrappers.<InStorageTp>query().lambda().eq(InStorageTp::getUniqueCode,
								wcsWmsTrx.getTrayNo()).ne(InStorageTp::getStatus,"2"));

				if(null == inStorageTp){

					log.error("此托盘"+wcsWmsTrx.getTrayNo()+"状态非法...");
					return  false;
				}

				InStorageItem inStorageItem =
						inStorageItemService
								.getOne(Wrappers.<InStorageItem>query().lambda().eq(InStorageItem::getInStorageItemId,
										inStorageTp.getInStorageItemId()));

				InStorage instorage =
						inStorageService.getOne(Wrappers.<InStorage>query().lambda().eq(InStorage::getWmsBillNo,
								inStorageItem.getWmsBillNo()));

                //更新库位信息
				sLocationTO.setTrayNo(wcsWmsTrx.getTrayNo());
				sLocationTO.setBatchNo(inStorageItem.getBatchNo());
				String pici = LocalDateTime.now()
						.format(DateTimeFormatter.ofPattern(DatePattern.PURE_DATETIME_MS_PATTERN));
				sLocationTO.setBatchCi(pici);
				sLocationTO.setHuozId(instorage.getHuozId());
				sLocationTO.setRealHuoz("1001");
				sLocationTO.setStockNum(inStorageTp.getAmount());
				sLocationTO.setIsUsed("1");
				sLocationTO.setSku(inStorageItem.getSku());
				if("4".equals(wcsWmsTrx.getTrxType())){
					//暂时取消空框货物状态'5'
					sLocationTO.setLocationStatus("4");
				}else{
					sLocationTO.setLocationStatus("4");
				}
				sLocationTO.setInPageNo(instorage.getInPageNo());
				sLocationTO.setInStorageItemId(inStorageItem.getInStorageItemId());
				sLocationService.updateById(sLocationTO);

				int sumAmount = inStorageItem.getRealAmount() + inStorageTp.getAmount();
				inStorageItem.setRealAmount(sumAmount);
				inStorageItemService.updateById(inStorageItem);

				//更新入库单信息 产线一次回调就更新成已完成
				instorage.setStatus("2");
				inStorageService.updateById(instorage);
				log.info("已更新入库单为入库完成：入库单编号："+instorage.getWmsBillNo());

				//更新库区状态
				//查找此库位所属库区是否有可使用的库位
				SLocation sLocation =
						sLocationService.getOne(Wrappers.<SLocation>query().lambda().eq(SLocation::getStatus, 1)
								.eq(SLocation::getAreaId, 1).eq(SLocation::getIsUsed, 0)
								.eq(SLocation::getStockAreaNo,sLocationTO.getStockAreaNo())
								.eq(SLocation::getLocationStatus,"1")
								.orderByAsc(SLocation::getSOrder).last("limit 1"));
				if(null == sLocation){
					//如果没有可使用的库位，更新库区为锁定状态
					StockArea stockArea = stockAreaService.getById(sLocationTO.getStockAreaNo());
					stockArea.setStatus("2");
					stockArea.setLockStatus("1");
					stockAreaService.updateById(stockArea);
				}else{

					//如果有可使用的库位，更新库区为未锁定状态
					StockArea stockArea = stockAreaService.getById(sLocationTO.getStockAreaNo());
					stockArea.setStatus("1");
					stockArea.setLockStatus("0");
					stockAreaService.updateById(stockArea);

				}

				inStorageTp.setItemId(pici);
				inStorageTp.setLocation(wcsWmsTrx.getToLocation());
				if(!"8".equals(wcsWmsTrx.getTrxType())){

					inStorageTp.setAmount(inStorageItem.getAmount());
				}
				if("3".equals(wcsWmsTrx.getTrxType())){

					inStorageTp.setOperator("产线操作");
				}
				inStorageTp.setStatus("2");
				inStorageTpService.updateById(inStorageTp);

				//更新wmstrx信息
				wcsWmsTrx.setTrxStatus("1");
				this.updateById(wcsWmsTrx);

				//回调ERP：上架回调接口
				ERPInStorageDTO erpInStorageDTO = new ERPInStorageDTO();
				ERPInStorageItemDTO erpInStorageItemDTO = new ERPInStorageItemDTO();
				ERPInStorageTpDTO erpInStorageTpDTO = new ERPInStorageTpDTO();

				BeanUtils.copyProperties(inStorageTp, erpInStorageTpDTO);
				List<ERPInStorageTpDTO> erpInStorageTpDTOList = new ArrayList<>();
				erpInStorageTpDTOList.add(erpInStorageTpDTO);
				erpInStorageItemDTO.setErpInStorageTpDTOList(erpInStorageTpDTOList);
				BeanUtils.copyProperties(inStorageItem, erpInStorageItemDTO);
				DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

				String pdStr = inStorageItem.getProductDate().format(formatter);
				erpInStorageItemDTO.setProductDate(pdStr);

				String effStr = inStorageItem.getEffDate().format(formatter);
				erpInStorageItemDTO.setEffDate(effStr);

				List<ERPInStorageItemDTO> erpInStorageItemDTOS = new ArrayList<>();
				erpInStorageItemDTOS.add(erpInStorageItemDTO);
				erpInStorageDTO.setErpInStorageItemDTOList(erpInStorageItemDTOS);
				BeanUtils.copyProperties(instorage, erpInStorageDTO);

				String  inDateStr = instorage.getInDate().format(formatter);
				erpInStorageDTO.setInDate(inDateStr);

				erpCallBackService.callBackIn(erpInStorageDTO);
				log.info("调用ERP上架回调完成");
				return true;

			}else if("7".equals(wcsWmsTrx.getTrxType())){
				//托盘出库
				sLocationFrom.setIsUsed("0");
				sLocationFrom.setTrayNo(null);
				sLocationFrom.setLocationStatus("1");
				//更新库位信息
				sLocationService.updateById(sLocationFrom);

				//更新wmstrx信息
				wcsWmsTrx.setTrxStatus("1");
				this.updateById(wcsWmsTrx);
				return true;
			} else if("5".equals(wcsWmsTrx.getTrxType())){
				//空托补给 PDA
				sLocationFrom.setIsUsed("0");
				sLocationFrom.setSku(null);
				sLocationFrom.setLocationStatus("1");
				//更新库位信息
				sLocationService.updateById(sLocationFrom);

				//更新wmstrx信息
				wcsWmsTrx.setTrxStatus("1");
				this.updateById(wcsWmsTrx);
				return true;

			} else if("9".equals(wcsWmsTrx.getTrxType())){
				//产线出库 释放当前库位
				sLocationFrom.setTrayNo(null);
				sLocationFrom.setBatchNo("");
				sLocationFrom.setBatchCi("");
				sLocationFrom.setSku(null);
				sLocationFrom.setHuozId(null);
				sLocationFrom.setRealHuoz(null);
				sLocationFrom.setStockNum(0);
				sLocationFrom.setOutAmount(0);
				sLocationFrom.setIsUsed("0");
				sLocationFrom.setLocationStatus("1");
				sLocationFrom.setInPageNo("");
				sLocationFrom.setInStorageItemId(0);

				//更新库位信息
				sLocationService.updateById(sLocationFrom);
				log.info("已释放"+sLocationFrom.getSLocationNo()+"库位");
				//检查库区情况是否释放库区
				StockArea stockArea = stockAreaService.getById(sLocationFrom.getStockAreaNo());

				log.info("检查"+stockArea.getStockAreaNo()+"库区是否可以释放");
				List<SLocation> sLocationList =
						sLocationService.list(Wrappers.<SLocation>query().lambda().eq(SLocation::getStockAreaNo,
								stockArea.getStockAreaNo())
								.eq(SLocation::getIsUsed, "1")
								.eq(SLocation::getLocationStatus,"4")
								.eq(SLocation::getStatus, "1"));

				if(null == sLocationList || sLocationList.size() == 0){

					stockArea.setStatus("0");
					stockArea.setLockStatus("0");
					stockAreaService.updateById(stockArea);
					log.info(stockArea.getStockAreaNo()+"库区没有找到正在使用有托盘货物并激活的库位，已经释放库区");
				}else{
					log.info(stockArea.getStockAreaNo()+"库区找到正在使用有托盘货物并激活的库位，不可以释放");
				}

				//更新wmstrx信息
				wcsWmsTrx.setTrxStatus("1");
				this.updateById(wcsWmsTrx);

				return true;

			} else if("10".equals(wcsWmsTrx.getTrxType())){
				//产线返库
				TraySku traySku = traySkuService.getById(wcsWmsTrx.getTrayNo());
				sLocationTO.setTrayNo(wcsWmsTrx.getTrayNo());
				sLocationTO.setBatchNo(traySku.getBatchNo());
				sLocationTO.setBatchCi(traySku.getBatchCi());
				sLocationTO.setSku(traySku.getSku());
				sLocationTO.setHuozId(traySku.getHuozId());
				sLocationTO.setRealHuoz(traySku.getRealHuoz());
				sLocationTO.setStockNum(traySku.getStockNum());
				sLocationTO.setIsUsed("1");
				sLocationTO.setLocationStatus("4");
				sLocationTO.setInPageNo(traySku.getInPageNo());
				sLocationTO.setInStorageItemId(traySku.getInStorageItemId());
				//更新库位信息
				sLocationService.updateById(sLocationTO);
				//移除临时托盘信息
				traySkuService.removeById(wcsWmsTrx.getTrayNo());
				//更新wmstrx信息
				wcsWmsTrx.setTrxStatus("1");

				//检查库区情况是否是锁定状态
				StockArea stockArea = stockAreaService.getById(sLocationTO.getStockAreaNo());

				log.info("托盘返库 检查"+stockArea.getStockAreaNo()+"库区是否锁定");

				if("1".equals(stockArea.getLockStatus())){
					log.info("托盘返库 库区是锁定状态 不必更新");
				}else{
					log.info(stockArea.getStockAreaNo()+"库区不是锁定状态，重新设置库区为满库和锁定状态");
					stockArea.setStatus("2");
					stockArea.setLockStatus("1");
					stockAreaService.updateById(stockArea);
				}

				this.updateById(wcsWmsTrx);

				return true;
			} else if("11".equals(wcsWmsTrx.getTrxType())){
				//更新wmstrx信息
				wcsWmsTrx.setTrxStatus("1");
				this.updateById(wcsWmsTrx);

				return true;
			} else if("12".equals(wcsWmsTrx.getTrxType())){
				//补货出库 释放当前库位
				sLocationFrom.setTrayNo(null);
				sLocationFrom.setBatchNo("");
				sLocationFrom.setBatchCi("");
				sLocationFrom.setSku(null);
				sLocationFrom.setHuozId(null);
				sLocationFrom.setRealHuoz(null);
				sLocationFrom.setStockNum(0);
				sLocationFrom.setIsUsed("0");
				sLocationFrom.setLocationStatus("1");
				sLocationFrom.setInPageNo("");
				sLocationFrom.setInStorageItemId(0);

				//更新库位信息
				sLocationService.updateById(sLocationFrom);

				//更新wmstrx信息
				wcsWmsTrx.setTrxStatus("1");
				this.updateById(wcsWmsTrx);
				return true;
			}

			//如果wcs返回失败
		} else if(wcsWmsCallBackDTO.getStatusCode() == 6){

			//更新wmstrx信息
			wcsWmsTrx.setTrxStatus("2");
			this.updateById(wcsWmsTrx);

		}

		return false;
	}

}
